package com.zhisheng.service.impl;

import com.zhisheng.mapper.DeptMapper;
import com.zhisheng.pojo.Dept;
import com.zhisheng.pojo.DeptLog;
import com.zhisheng.service.DeptLogService;
import com.zhisheng.service.DeptService;
import com.zhisheng.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

@Service
@Primary
public class DeptServiceImpl2 implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpService empService;

    @Autowired
    private DeptLogService deptLogService;

    @Override
    public List<Dept> selectAll() {
        int i = 1/0;
        return deptMapper.selectAll();
    }

    // 删除部门的同时,也要删除这个部门下面所有的员工
    @Override
    @Transactional(rollbackFor = ParseException.class)
    public int delById(Integer deptId) throws ParseException {
        int i = deptMapper.delById(deptId);
        //int a = 1/0; // 出现运行时期的异常,spring会自动回滚
        // 默认情况下,出现了受检异常,不会回滚
        int i2= empService.delByDeptId(deptId);
        deptLogService.insert(new DeptLog(null,LocalDateTime.now(),i+","+i2));
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date = sdf.parse("sduifyt87634hgjsdf");

        return i;
    }

    @Override
    public int insertDept(Dept dept) {
        // 给dept对象补充两个数据
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        return deptMapper.insertDept(dept);
    }
}
